/**
 * 枚举 0 到 sqrt(b1) 中 b1 的因子(也就是 x )，
 * 如果这个数是 a1 的整数倍，并且满足
 * gcd(x/a1, a0/a1) = 1
 * gcd(b1/b0, b1/x) = 1
 * 则ans++
 *
 * ref: https://blog.csdn.net/nuclearsubmarines/article/details/77603154
 */

#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    int a0, a1, b0, b1;
    cin >> a0 >> a1 >> b0 >> b1;
    int p = a0 / a1, q = b1 / b0, ans = 0;
    for (int x = 1; x <= b1 / x; ++x) {
      if (b1 % x == 0) {
        if (x % a1 == 0 && __gcd(x / a1, p) == 1 && __gcd(q, b1 / x) == 1)
          ans++;
        int y = b1 / x;
        if (x == y) continue;
        if (y % a1 == 0 && __gcd(y / a1, p) == 1 && __gcd(q, b1 / y) == 1)
          ans++;
      }
    }
    cout << ans << endl;
  }
}
